package com.kellycheung.chicken_go_go;

import java.util.HashMap;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class PlayerStatusObj extends BaseObject {
	public class PlayerStatus {
		protected int iIdx;
		protected int iColor;
		protected float fProgress;
		protected float fProgressTtl;
		
		public PlayerStatus( int p_iIdx , float p_fProgressTtl ) {
			try {
				this.iIdx = p_iIdx;
				this.iColor = PlayerStatusObj.I_COLOR_LIST[this.iIdx];
				this.fProgress = 0f;
				this.fProgressTtl = p_fProgressTtl;
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "PlayerStatus" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public void destroy() {
		}
		
		public void setProgress( float p_fProgress ) {
			try {
				this.fProgress = p_fProgress;
				
				if ( this.fProgress > this.fProgressTtl ) {
					this.fProgress = this.fProgressTtl;
				}
			} catch ( Exception p_e ) {
				// TODO Auto-generated catch block
				GameUtil.LogMsg( this , "PlayerStatus.setProgress" );
				GameUtil.ErrorHandler( this , p_e );
			}
		}
		
		public boolean isPlayerFinished() {
			return this.fProgress >= this.fProgressTtl;
		}
	}
	
	public static final int[] I_COLOR_LIST = new int[]{ Color.RED , Color.GREEN , Color.BLUE , Color.YELLOW };
	
	protected GRectangle grBound;
	
	protected HashMap<String, PlayerStatus> hmPlayer;
	
	protected int iPlayerCnt;
	protected float fWidth;
	protected float fHeightTtl;
	protected float fHeightBar;
	protected float fHeightSpace;
	
	public PlayerStatusObj( GameScene p_gsScene , int p_iZOrder , GOAnchor p_goa ) {
		this( p_gsScene , p_iZOrder , p_goa , 0f , 0f , p_gsScene.geMain.gcScreenSize.fX , p_gsScene.geMain.gcScreenSize.fY , 12f );
	}
	
	public PlayerStatusObj( GameScene p_gsScene , int p_iZOrder , GOAnchor p_goa , float p_fX , float p_fY , float p_fW , float p_fHTtl , float p_fHBar ) {
		super( p_gsScene , p_iZOrder , p_goa , p_fX , p_fY );
		// TODO Auto-generated constructor stub
		
		try {
			this.iPlayerCnt = 0;
			this.hmPlayer = new HashMap<String, PlayerStatus>();
			this.fWidth = p_fW;
			this.fHeightTtl = p_fHTtl;
			this.fHeightBar = p_fHBar;
			this.fHeightSpace = 12f;
			
			this.grBound = new GRectangle( this.gcCoord , this.gcCoord.offset( p_fW , p_fHTtl ) );
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	@Override
	public void destroy() {
		try {
			if ( this.hmPlayer != null ) {
				for ( PlayerStatus ps : this.hmPlayer.values() ) {
					ps.destroy();
				}
				
				this.hmPlayer.clear();
				this.hmPlayer = null;
			}
			
			this.grBound = null;
			
			super.destroy();
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public int addPlayer( float p_fProgressTtl ) {
		try {
			return this.addPlayer( String.valueOf(this.hmPlayer.size() + 1) , p_fProgressTtl );
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj.addPlayer" );
			GameUtil.ErrorHandler( this , p_e );
		}
		
		return 0;
	}
	
	public void changeColor( String p_sName , int p_iColor ) {
		try {
			this.hmPlayer.get( p_sName ).iColor = PlayerStatusObj.I_COLOR_LIST[ p_iColor ];
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj.changeColor" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public int addPlayer( String p_sName , float p_fProgressTtl ) {
		try {
			if ( this.hmPlayer != null ) {
				this.hmPlayer.put( p_sName , new PlayerStatus( Integer.valueOf(p_sName.substring( p_sName.length()-1 , p_sName.length() )) - 1 , p_fProgressTtl ) );
				
				return this.hmPlayer.get( p_sName ).iIdx;
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj.addPlayer" );
			GameUtil.ErrorHandler( this , p_e );
		}
		
		return -1;
	}
	
	public void setPlayerProgress( String p_sName , float p_fProgress ) {
		try {
			if ( this.hmPlayer.containsKey( p_sName ) ) {
				this.hmPlayer.get( p_sName ).setProgress( p_fProgress );
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj.setPlayerProgress" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public boolean isPlayerFinished( String p_sName ) {
		try {
			if ( this.hmPlayer.containsKey( p_sName ) ) {
				return this.hmPlayer.get( p_sName ).isPlayerFinished();
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj.isPlayerFinished" );
			GameUtil.ErrorHandler( this , p_e );
		}
		
		return false;
	}
	
	@Override
	public void draw( Canvas p_c , Paint p_p ) {
		try {
			GCoordinate gcOrig = new GCoordinate( GOAnchor.convCoord( this.goaAnchor , this.gcCoord , this.grBound )  );
			GCoordinate gcBarHW = new GCoordinate( this.fWidth , this.fHeightBar );
			GCoordinate gcSpace = new GCoordinate( 0f , this.fHeightSpace );
			GRectangle gr = new GRectangle( 0f , 0f , 0f , 0f ); 
			Paint p = new Paint( p_p );
			
			for ( PlayerStatus ps : this.hmPlayer.values() ) {
				if ( ps != null ) {
					p.setColor( Color.BLACK );
					gr.set( gcOrig.fX + gcBarHW.fX , gcOrig.fY , gcOrig.fX , gcOrig.fY + gcBarHW.fY );
					p_c.drawRect( gr.toRect() , p );
					
					p.setColor( ps.iColor );
					gr.set( gcOrig.fX+1 + (gcBarHW.fX-2)*(ps.fProgress/ps.fProgressTtl) , gcOrig.fY+1 , gcOrig.fX+1 , gcOrig.fY + gcBarHW.fY - 2 );
					p_c.drawRect( gr.toRect() , p );
					
					gcOrig.add( gcSpace );
				}
			}
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj.draw" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}

	@Override
	public void preGameLoopLogic() {
		// TODO Auto-generated method stub
		try {
			this.grBound.set( this.gcCoord , this.gcCoord.offset( this.fWidth , this.fHeightTtl ) );
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "PlayerStatusObj.preGameLoopLogic" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}

	@Override
	public void gameLoopLogic() {
	}

	@Override
	public void postGameLoopLogic() {
	}
}